﻿using Framework.EncryptionDecryptionService.Enums;
using Framework.EncryptionDecryptionService.Helpers;
using Framework.EncryptionDecryptionService.Inteface;
using Framework.EncryptionDecryptionService.Models;
using System.Text.Json.Serialization;

namespace Framework.EncryptionDecryptionService.Abstract
{
    /// <summary>
    /// 解密服务抽象
    /// </summary>
    public abstract class AbsDecryptionService : IDecryptionService
    {
        /// <summary>
        /// 秘钥
        /// </summary>
        internal readonly ISecretKeyService SecretKey;

        public SecretKeyModel GetSecretKey { get { return new SecretKeyModel(SecretKey?.PrivateSecretKey, SecretKey?.PublicSecretKey); } }

        /// <summary>
        /// 解密服务抽象
        /// </summary>
        /// <param name="secretKey">秘钥</param>
        public AbsDecryptionService(ISecretKeyService secretKey)
        {
            SecretKey = secretKey;
        }

        public virtual T Decrypt<T>(string ciphertext)
        {
            return DecryptToString(ciphertext.FromBase64String()).ConvertToObject<T>();
        }


        public virtual T Decrypt<T>(byte[] ciphertext)
        {
            return DecryptToString(ciphertext).ConvertToObject<T>();
        }


        public virtual string Decrypt(string ciphertext)
        {
            return DecryptToString(ciphertext.FromBase64String());
        }

        public abstract byte[] Decrypt(byte[] encryptedBytes);

        public virtual string DecryptToString(byte[] encryptedBytes)
        {
            return Decrypt(encryptedBytes).ConvertToString();
        }
    }
}
